<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/24 0024
 * Time: 下午 3:03
 */

namespace App\Model;
use App\Utility\Pool\MySQLPool;
use App\Utility\Pool\MySQLObject;
use EasySwoole\Component\Pool\PoolManager;
use App\Utility\BaseEnum;

class BaseModel
{
    protected $db;           //DB对象
    public function __construct(){
        $this->db = PoolManager::getInstance()->getPool(MySQLPool::class)->getObj();
        //TODO 单独的实例化对象
        if(!$this->db || empty($this->db)){
            $dbconfig = require_once EASYSWOOLE_ROOT.'/Config/comm.php'; //自定义对象连接
            $dbconfig = !empty($dbconfig["MYSQL"]) ? $dbconfig["MYSQL"] : FALSE;
            $this->db = new \MysqliDb($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['database']);
            $this->db->autoReconnect = TRUE; 
        }
    }
    
    
    /**
     * TODO 连接池释放
     * @return MySQLObject
     */
    protected function getDb(){
        return $this->db;
    }
    
    //连接对象
    public  function dbConnector(){
        return $this->db;
    }
    
    
    
    //无效方法
    public function clearPool(){
    }
    
    /**
     * TODO 连接池释放
     */
    public function __destruct()
    {
        PoolManager::getInstance()->getPool(MySQLPool::class)->recycleObj($this->db);
        BaseEnum::Logger("BaseModel___destruct  连接池释放","user-login");
    }
    
}



